//
//  flii.m
//  flii
//
//  Created by Pete Hobson on 06/01/2007.
//  Copyright 2007 freesome.com All rights reserved.
//
//  Neko extension for osx


#include "flii.h"

static value *TXTStorage;
static value *IRStorage;
static value *ACStorage;
static value *JSStorage;
static value *BUTStorage;
static value *ACCABStorage;
static value *JOYCABStorage;

static bridge *flii;

//Primitive to set the framework callback functions
void set_handler( value cb, value ir, value ac,value js, value but  ) {

	NSLog(@"set_handler");
	TXTStorage = alloc_root(1);
   *TXTStorage = cb;
	IRStorage = alloc_root(1);
   *IRStorage = ir;
	ACStorage = alloc_root(1);
   *ACStorage = ac;  
	JSStorage = alloc_root(1);
   *JSStorage = js; 
	BUTStorage = alloc_root(1);
   *BUTStorage = but;
   }
void set_calibration(value acb, value jcb) {
	ACCABStorage = alloc_root(1);
   *ACCABStorage = acb;
	JOYCABStorage = alloc_root(1);
   *JOYCABStorage = jcb;

  
}

//Functions to call the callbacks
void  sendIR(float x, float y) {
	val_call2(*IRStorage,alloc_float(x), alloc_float(y));
}
void  sendAC(UInt16 type,int x, int y, int z){
	value args[] = {
		alloc_int(type),
		alloc_int(x),
		alloc_int(y),
		alloc_int(z)};
		
	val_callN(*ACStorage, args,4);
	
}
void sendJS(int type, int x, int y) {
	
	value args[] = {
		alloc_int(type),
		alloc_int(x),
		alloc_int(y)};
	val_callN(*JSStorage, args,3);
	//sendInts();
}

void sendBUT( int type, BOOL isPressed) {
	val_call2(*BUTStorage, alloc_int(type), alloc_bool(isPressed));
}
void  sendInts() {
	NSLog(@"sendInts");
	value parr=alloc_array(2);
	val_array_ptr(parr)[0] = alloc_int(0);
	val_array_ptr(parr)[1] = alloc_int(1);
	//val_call1(*sendIntsStorage,parr);
}	


void  sendCallback(char * o) {
	NSLog(@"sendMsg");
	
 val_call1(*TXTStorage,alloc_string(o));
}	

void sendACalibration(int type, int accX_zero, int accY_zero,int accZ_zero,int  accX_1g,int accY_1g,int accZ_1g){
 	value args[] = {
		alloc_int(type),
		alloc_int(accX_zero),
		alloc_int(accY_zero),
		alloc_int(accZ_zero),
		alloc_int(accX_1g),
		alloc_int(accY_1g),
		alloc_int(accZ_1g)
		};
		val_callN(*ACCABStorage, args,7);
 NSLog(@"callibration %d %d %d %d %d %d %d",type, accX_zero, accY_zero, accZ_zero,  accX_1g, accY_1g, accZ_1g );
 
}

void sendJoyCalibration(int type,int x_max,int x_min,int x_center,int y_max,int y_min,int y_center) {
 	value args[] = {
		alloc_int(type),
		alloc_int(x_max),
		alloc_int(x_min),
		alloc_int(x_center),
		alloc_int(y_max),
		alloc_int(y_min),
		alloc_int(y_center)
		};
		val_callN(*JOYCABStorage, args,7);
}


 
//Main entry primitive for haXe
//Does not return
value beginWii() {

	startDiscovery();	

	CFRunLoopRun();
	//This is never called
	return alloc_string("Flli.started");
}
void killWii() {
  CFRunLoopStop(CFRunLoopGetCurrent()); 
  [flii killMe];
  NSLog(@"WiiKilled");
}

//Set the wii discovery in motion
void startDiscovery() {
	
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	flii = [[bridge alloc] init];
	[flii retain];
	[flii start];
	if (FALSE) [pool release];	
	
}
int main() {

	//set_handler( value cb, value ir, value ac,value js, value but )
	beginWii();
	return(1);
}

DEFINE_PRIM(set_handler,5);
DEFINE_PRIM(set_calibration,2);
DEFINE_PRIM(beginWii,0); 
DEFINE_PRIM(killWii,0); 

